<?php
declare (strict_types=1);

namespace app\admin\logic\finance;

use app\admin\logic\system\Log;
use app\common\model\payment\admin\PaymentOrderItem;
use app\common\queue\payment\Payment;
use think\db\exception\DbException;
use think\facade\Request;
use think\Paginator;

class OrderFail extends PaymentOrderItem
{
    /**
     * getFailList
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 24/07/25
     *
     * @return Paginator
     * @throws DbException
     */
    public function getFailList()
    {
        // 定义
        $map = [];
        // 手机号
        Request::has('phone', 'get', true) && $map[] = ['user.phone', '=', Request::get('phone/s')];
        // 时间
        if (Request::has('time', 'get', true)) {
            $time  = Request::get('time/a');
            $stime = strtotime(date('Y-m-d 00:00:00', strtotime($time[0])));
            $etime = strtotime(date('Y-m-d 00:00:00', strtotime($time[1]))) + 86399;
            $map[] = ['__TABLE__.create_time', 'between', [$stime, $etime]];
        }
        // 订单号
        Request::has('order_no', 'get', true) && $map[] = ['__TABLE__.order_sub_no|__TABLE__.order_no', '=', Request::get('order_no/s')];

        return $this->field('order_no,order_sub_no,product_id,product_type_id,payment,mhm_id,user_id,create_time')
            ->withJoin(['main', 'expenditure', 'user'])
            ->with(['user_phone', 'user_name', 'payment_type_text', 'school_name'])
            ->where($map)
            ->where('main.status', 10)
            ->order('__TABLE__.create_time', 'DESC')
            ->hidden(['main', 'expenditure', 'user'])
            ->paginate($this->getListRows())->append(['product_type', 'product_title']);

    }

    /**
     * HandleFailOrder
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 24/07/25
     *
     * @param $orderNo
     */
    public function HandleFailOrder($orderNo)
    {
        app(Payment::class)->run($orderNo);

        Log::write('处理失败订单【' . $orderNo . '】', 'order', 'update', true);
    }

}
